/*
 * Created on Apr 17, 2004
 *
 */
package org.vectrics.recruiter.job;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.common.db.Criteria;
import org.vectrics.company.Company;
import org.vectrics.domain.Domain;
import org.vectrics.location.Location;
import org.vectrics.user.Person;

/**
 * @author mmoore
 */
public class JobSearchCriteria extends Criteria {

	private static final long serialVersionUID = -6508544911997716289L;
	
	private Collection statusTypeCodes = new HashSet();
	private Long candidateId = null;
	private Collection searchWords = new HashSet();
	private Collection<Location> locations = new HashSet<Location>();
	private Collection jobFunctions = new HashSet();
	private Date earliestDate = null;
	private Date latestDate = null;
	private Date earliestCreateDate = null;
	private Date latestCreateDate = null;
	private Set companies = null;
	private Person person = null;
	private Set referenceCodes = new HashSet();
	private boolean jobsNeedingApproval = false;	
	private Set subStatusTypeCodes = new HashSet();
	private String title = null;
	private Long primaryRecruiter = null;
	private Long requester = null;
	
	/**
	 * Cosntructor 
	 */
	public JobSearchCriteria() {
		super();
	}
	
	public Set getSubStatusTypeCodes() {
		return subStatusTypeCodes;
	}

	public void addSubStatusTypeCode(String code) {
		this.subStatusTypeCodes.add(code);
	}

	public void setSubStatusTypeCodes(Set subStatusTypeCodes) {
		this.subStatusTypeCodes = subStatusTypeCodes;
	}

	public void addCompany(Company company) {
		if (company == null)
			throw new DataException("Company id added to criteria was passed as NULL");
		if (companies == null)
			companies = new HashSet();
		this.companies.add(company);
	}
	
	public Set getCompanies() {
		return(companies);
	}
	
	public void setCompanies(Set companies) {
		this.companies = companies;
	}

	/**
	 * @return Returns the candidateId.
	 */
	public Long getCandidateId() {
		return candidateId;
	}

	/**
	 * @param candidateId The candidateId to set.
	 */
	public void setCandidateId(Long candidateId) {
		this.candidateId = candidateId;
	}

	/**
	 * @return Returns the earliestDate.
	 */
	public Date getEarliestPostDate() {
		return earliestDate;
	}
	/**
	 * @param earliestDate The earliestDate to set.
	 */
	public void setEarliestPostDate(Date earliestDate) {
		this.earliestDate = earliestDate;
	}
	/**
	 * @return Returns the latestDate.
	 */
	public Date getLatestPostDate() {
		return latestDate;
	}
	/**
	 * @param latestDate The latestDate to set.
	 */
	public void setLatestPostDate(Date latestDate) {
		this.latestDate = latestDate;
	}
	/**
	 * @param searchWords The searchWords to set.
	 */
	public void setSearchWords(Collection searchWords) {
		this.searchWords = searchWords;
	}

	public void setSearchWords(String words) {
		this.searchWords.clear();
		if (words == null) {
			// No words to parse
		} else {
			// Parse all words
			String delimiter = " ";
			String temp = words.trim();
			int pos = temp.indexOf(delimiter);
			while ((pos >= 0) && (temp.length() > 0)) {
				String word  = temp.substring(0, pos);
				if (word.length() > 0) {
					searchWords.add(word);
				}
				if (temp.length() > pos + 1) {
					temp = temp.substring(pos + 1);
				} else 
					temp = "";
				pos = temp.indexOf(delimiter);
			}
			if (temp.trim().length() > 0) {
				searchWords.add(temp);
			}
		}
	}
	
	/**
	 * @return Returns the searchWords.
	 */
	public Collection getSearchWords() {
		return this.searchWords;
	}
	
	public void addSearchWord(String word) {
		this.searchWords.add(word);
	}
	
	/**
	 * @return Returns the jobFunctions.
	 */
	public Collection getJobFunctions() {
		return jobFunctions;
	}
	/**
	 * @param jobFunctions The jobFunctions to set.
	 */
	public void setJobFunctions(Collection jobFunctions) {
		this.jobFunctions = jobFunctions;
	}
	
	/*
	 * 
	 */
	public void addJobFunction(JobFunction jobFunction) {
		this.jobFunctions.add(jobFunction);
	}
	
	/**
	 * @return Returns the locations.
	 */
	public Collection<Location> getLocations() {
		return locations;
	}
	
	/**
	 * @param locations The locations to set.
	 */
	public void setLocations(Collection<Location> locations) {
		this.locations = locations;
	}
	
	/*
	 * Add a location for criteria
	 */
	public void addLocation(Location location) {
		this.locations.add(location);
	}
	
	/**
	 * @return Returns the statusTypes.
	 */
	public Collection getStatusTypes() {
		return statusTypeCodes;
	}
	/**
	 * @param statusTypes The statusTypes to set.
	 */
	public void setStatusTypes(Collection statusTypeCodes) {
		this.statusTypeCodes = statusTypeCodes;
	}
	
	public void addStatusTypeCode(String statusTypeCode) {
		this.statusTypeCodes.add(statusTypeCode);
	}

	/**
	 * @return Returns the person.
	 */
	public Person getPerson() {
		return person;
	}

	/**
	 * @param person The person to set.
	 */
	public void setPerson(Person person) {
		this.person = person;
	}

	public String toString() {
		StringBuffer str = new StringBuffer();
		Iterator iter = this.getDomains().iterator();
		str.append("domains={");
		while (iter.hasNext()) {
			Domain domain = (Domain)iter.next();
			str.append(domain);
			if (iter.hasNext())
				str.append("|");
		}
		str.append("}");

		str.append(" referenceIds={");
		iter = this.getReferenceCodes().iterator();
		while (iter.hasNext()) {
			String refId = (String)iter.next();
			str.append(refId);
			if (iter.hasNext())
				str.append("|");
		}
		str.append("}");
		
		str.append(" candidateId={" + this.getCandidateId() + "}");
		str.append(" voidType={" + this.getVoidedType() + "}");
		str.append(" earliestPostDate={" + this.getEarliestPostDate() + "}");
		str.append(" latestPostDate={" + this.getLatestPostDate() + "}");
		str.append(" earliestCreateDate={" + this.getEarliestCreateDate() + "}");
		str.append(" latestCreateDate={" + this.getLatestCreateDate() + "}");
		str.append(" jobFunctions={" + this.getJobFunctions() + "}");
		str.append(" locations={" + this.getLocations() + "}");
		str.append(" person={" + this.getPerson() + "}");
		str.append(" statusTypes={" + this.getStatusTypes() + "}");
		str.append(" subStatusTypesCodes={" + this.getSubStatusTypeCodes() + "}");
		str.append(" searchWords={" + this.getSearchWords() + "}");
		
		return(str.toString());
	}

	/**
	 * @return Returns the earliestCreateDate.
	 */
	public Date getEarliestCreateDate() {
		return earliestCreateDate;
	}

	/**
	 * @param earliestCreateDate The earliestCreateDate to set.
	 */
	public void setEarliestCreateDate(Date earliestCreateDate) {
		this.earliestCreateDate = earliestCreateDate;
	}

	/**
	 * @return Returns the latestCreateDate.
	 */
	public Date getLatestCreateDate() {
		return latestCreateDate;
	}

	/**
	 * @param latestCreateDate The latestCreateDate to set.
	 */
	public void setLatestCreateDate(Date latestCreateDate) {
		this.latestCreateDate = latestCreateDate;
	}

	public void addReferenceCode(String refCode) {
		if ((refCode == null) || (refCode.trim().length() == 0))
			throw new SystemException("Attempt to add a refereceId to the criteria that is null or blank");
		this.referenceCodes.add(refCode);
	}
	
	/**
	 * @return Returns the referenceCodes.
	 */
	public Set getReferenceCodes() {
		return referenceCodes;
	}

	/**
	 * @param referenceCodes The referenceCodes to set.
	 */
	public void setReferenceCodes(Set referenceCodes) {
		this.referenceCodes = referenceCodes;
	}

	/**
	 * @return Returns the jobsNeedingApproval.
	 */
	public boolean getJobsNeedingApproval() {
		return jobsNeedingApproval;
	}

	/**
	 * @param jobsNeedingApproval The jobsNeedingApproval to set.
	 */
	public void setJobsNeedingApproval(boolean jobsNeedingApproval) {
		this.jobsNeedingApproval = jobsNeedingApproval;
	}
	
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the primaryRecruiter
	 */
	public Long getPrimaryRecruiter() {
		return primaryRecruiter;
	}

	/**
	 * @param primaryRecruiter the primaryRecruiter to set
	 */
	public void setPrimaryRecruiter(Long primaryRecruiter) {
		this.primaryRecruiter = primaryRecruiter;
	}

	/**
	 * @return the requester
	 */
	public Long getRequester() {
		return requester;
	}

	/**
	 * @param requester the requester to set
	 */
	public void setRequester(Long requester) {
		this.requester = requester;
	}
	
}
